package com.cs2340.taskplusplus;

import java.text.DateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This is the UI for the Task.
 * @author GRILLENBANNER
 *
 */

public class TaskScreen extends Activity implements OnClickListener 
{	
	private Task task;
	private Intent intent;
	/**
	 * This creates the activity, populating the fields with the task's data.
	 * @param b
	 */
	public void onCreate(Bundle b)
	{
		super.onCreate(b);
		setContentView(R.layout.taskview);
        View addLabel = findViewById(R.id.addLabel_taskview);
        addLabel.setOnClickListener(this);

		String taskname = this.getIntent().getExtras().getString("Task Name");
		String parentname = this.getIntent().getExtras().getString("Task Parent Table");
		task = new Task(parentname, taskname);
		
		//populate name field
		TextView curr = (TextView) this.findViewById(R.id.taskview_TaskName);
		curr.setText(task.getName());
		
		//populate description field
		curr = (TextView) this.findViewById(R.id.taskview_Desc);
		curr.setText(task.getDescription());
		
		//populate location field
		curr = (TextView) this.findViewById(R.id.taskview_location);
		curr.setText(DateFormat.getDateInstance().format(task.getDate()));
		
		//button to go to map
		View mapViewB = findViewById(R.id.mapViewMarkedButton_taskview);
		mapViewB.setOnClickListener(this);
		
		
		/*
		 * GET TASK TO USE A LOCATION
		 * THEN REPLACE THE COMMENTED CODE WITH THE UNCOMMENTED CODE
		 * AND EVERYTHING SHOULD WORK
		 * 
		 */
		
		Location loc = task.getLocation();
		CurrentLocation.setLat(loc.getLat());
		CurrentLocation.setLon(loc.getLon());
		CurrentLocation.setName(task.getName());
		
		//populate categories field
		displayLabels();
	}
	
	/**
	 * Method to populate the category field in the activity.
	 */
	public void displayLabels()
	{
		TextView curr = (TextView) this.findViewById(R.id.taskview_categories);
		ArrayList<Label> labels = (ArrayList<Label>) task.getLabels();
		if (labels.size() == 0)
			curr.setText("No Labels   ");
		else
			curr.setText("");
		
		for (Label l : labels)
		{
			System.out.println(l.getName());
			curr.append(l.getName()+", ");
		}
		if (curr.getText().length() > 0)
			curr.setText(curr.getText().subSequence(0, curr.getText().length()-2));	
		
	}
	
	/**
	 * listener method for buttons.
	 */
	public void onClick(View v) 
	{
		switch(v.getId())
		{
			case R.id.mapViewMarkedButton_taskview:
				intent = new Intent(this, MapViewMarkedDisplay.class);	
	    		startActivity(intent);
				break;
			case R.id.addLabel_taskview:
				EditText e = (EditText) this.findViewById(R.id.labelName_taskview);
				task.addLabel(new Label(task.getParentTable(), e.getText().toString()));
				e.setText("");
				displayLabels();
				break;
		}
	}
}
